import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Mou implements MouseListener {
    private int flag = 1;
    private final JButton button;
    private final Dimension size1 = new Dimension(150, 50);
    private final Dimension size2 = new Dimension(200, 70);

    public Mou(JButton button) {
        this.button = button;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println("按钮被点击了");
        JOptionPane.showMessageDialog(button, "你点击了按钮！", "提示", JOptionPane.INFORMATION_MESSAGE);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        System.out.println("鼠标按下");
        button.setBackground(Color.LIGHT_GRAY);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        System.out.println("鼠标松开");
        button.setBackground(null);
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        System.out.println("鼠标滑入按钮");
        if (flag == 1) {
            button.setPreferredSize(size2);
            button.setText("鼠标滑入了");
            flag = 2;
        } else {
            button.setPreferredSize(size1);
            button.setText("点击我");
            flag = 1;
        }
        button.getParent().revalidate();
        button.getParent().repaint();
    }

    @Override
    public void mouseExited(MouseEvent e) {
        System.out.println("鼠标滑出按钮");
        button.setPreferredSize(size1);
        button.setText("点击我");
        flag = 1;
        button.setBackground(null);
        button.getParent().revalidate();
        button.getParent().repaint();
    }
}
